package com.mdinic.blogovi.model;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

@RooJavaBean
@RooToString
@Configurable
@Entity
@RooJpaActiveRecord(finders = { "findEmailsByType", "findEmailsByActiveNot" })
public class Email {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Version
    @Column(name = "version")
    private Integer version;

    @NotNull
    private String name;

    @NotNull
    @Enumerated
    private EmailType type;

    @NotNull
    private String subject;

    @NotNull
    private String body;

    @NotNull
    private Boolean active;

    @PersistenceContext
    transient EntityManager entityManager;

    public static final EntityManager entityManager() {
        EntityManager em = new Email().entityManager;
        if (em == null)
            throw new IllegalStateException(
                    "Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

    public static long countEmails() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Email o", Long.class).getSingleResult();
    }

    public static List<com.mdinic.blogovi.model.Email> findAllEmails() {
        return entityManager().createQuery("SELECT o FROM Email o", Email.class).getResultList();
    }

    public static com.mdinic.blogovi.model.Email findEmail(Long id) {
        if (id == null)
            return null;
        return entityManager().find(Email.class, id);
    }

    public static List<com.mdinic.blogovi.model.Email> findEmailEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Email o", Email.class).setFirstResult(firstResult)
                .setMaxResults(maxResults).getResultList();
    }

    @Transactional
    public void persist() {
        if (this.entityManager == null)
            this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

    @Transactional
    public void remove() {
        if (this.entityManager == null)
            this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Email attached = findEmail(this.id);
            this.entityManager.remove(attached);
        }
    }

    @Transactional
    public void flush() {
        if (this.entityManager == null)
            this.entityManager = entityManager();
        this.entityManager.flush();
    }

    @Transactional
    public void clear() {
        if (this.entityManager == null)
            this.entityManager = entityManager();
        this.entityManager.clear();
    }

    @Transactional
    public com.mdinic.blogovi.model.Email merge() {
        if (this.entityManager == null)
            this.entityManager = entityManager();
        Email merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public EmailType getType() {
        return type;
    }

    public void setType(EmailType type) {
        this.type = type;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public Boolean getActive() {
        return active;
    }

    public void setActive(Boolean active) {
        this.active = active;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public static TypedQuery<Email> findEmailsByActiveNot(Boolean active) {
        if (active == null)
            throw new IllegalArgumentException("The active argument is required");
        EntityManager em = Email.entityManager();
        TypedQuery<Email> q = em.createQuery("SELECT o FROM Email AS o WHERE o.active IS NOT :active", Email.class);
        q.setParameter("active", active);
        return q;
    }

    public static TypedQuery<Email> findEmailsByType(EmailType type) {
        if (type == null)
            throw new IllegalArgumentException("The type argument is required");
        EntityManager em = Email.entityManager();
        TypedQuery<Email> q = em.createQuery("SELECT o FROM Email AS o WHERE o.type = :type", Email.class);
        q.setParameter("type", type);
        return q;
    }

    public static Email findActiveEmailByType(EmailType type) {
        if (type == null)
            throw new IllegalArgumentException("The type argument is required");
        EntityManager em = Email.entityManager();
        TypedQuery<Email> q = em.createQuery("SELECT o FROM Email AS o WHERE o.type = :type AND  o.active IS :active",
                Email.class);
        q.setParameter("type", type);
        q.setParameter("active", true);
        List<Email> resultList = q.getResultList();
        if (resultList.isEmpty()) {
            return null;
        } else {
            return resultList.get(0);
        }
    }

    @Override
    public String toString() {
        return "Email [name=" + name + ", type=" + type + "]";
    }
}
